package com.example.firstdemo.ui.screens.components

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Snackbar
import androidx.compose.material3.SnackbarData
import androidx.compose.material3.SnackbarDefaults
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.example.firstdemo.ui.theme.eColors

enum class SnackbarType {
    PRIMARY, INFO, SUCCESS, WARNING, ERROR
}

class TinyMessageBoxSate() {
    var type: SnackbarType = SnackbarType.PRIMARY
    val snackbarHostState = SnackbarHostState()

    suspend fun showSnackbar(
        type: SnackbarType = SnackbarType.INFO,
        message: String,
        actionLabel: String? = null,
        withDismissAction: Boolean = false,
        duration: SnackbarDuration =
            if (actionLabel == null) SnackbarDuration.Short else SnackbarDuration.Indefinite,
    ): SnackbarResult {
        closeSnackbar()
        this.type = type
        return snackbarHostState.showSnackbar(message, actionLabel, withDismissAction, duration)
    }

    suspend fun closeSnackbar() {
        snackbarHostState.currentSnackbarData?.dismiss()
    }
}

@Composable
fun MySnackbar(
    snackbarData: SnackbarData,
    modifier: Modifier = Modifier,
    actionOnNewLine: Boolean = false,
    shape: Shape = SnackbarDefaults.shape,
    containerColor: Color = SnackbarDefaults.color,
    contentColor: Color = SnackbarDefaults.contentColor,
    actionColor: Color = SnackbarDefaults.actionColor,
    actionContentColor: Color = SnackbarDefaults.actionContentColor,
    dismissActionContentColor: Color = SnackbarDefaults.dismissActionContentColor,
) {
    val actionLabel = snackbarData.visuals.actionLabel
    val actionComposable: (@Composable () -> Unit)? =
        if (actionLabel != null) {
            @Composable {
                TextButton(
                    colors = ButtonDefaults.textButtonColors(contentColor = actionColor),
                    onClick = { snackbarData.performAction() },
                    content = { Text(actionLabel) },
                )
            }
        } else {
            null
        }
    val dismissActionComposable: (@Composable () -> Unit)? =
        if (snackbarData.visuals.withDismissAction) {
            @Composable {
                IconButton(
                    onClick = { snackbarData.dismiss() },
                    content = {
                        Icon(
                            androidx.compose.material.icons.Icons.Filled.Close,
                            contentDescription = null,
                        )
                    },
                )
            }
        } else {
            null
        }
    Snackbar(
        modifier = modifier.padding(0.dp),
        action = actionComposable,
        dismissAction = dismissActionComposable,
        actionOnNewLine = actionOnNewLine,
        shape = shape,
        containerColor = containerColor,
        contentColor = contentColor,
        actionContentColor = actionContentColor,
        dismissActionContentColor = dismissActionContentColor,
        content = {
            Row(Modifier.padding(0.dp)) {
                Text(
                    text = snackbarData.visuals.message,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                    color = contentColor
                )
            }
        },
    )
}

@Composable
fun TinyMessageBox(tinyMessageBoxSate: TinyMessageBoxSate) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier.fillMaxWidth()
    ) {
        Spacer(Modifier.height(35.dp))
        SnackbarHost(
            hostState = tinyMessageBoxSate.snackbarHostState,
            modifier = Modifier.fillMaxWidth(0.9f)
        ) {
            when (tinyMessageBoxSate.type) {
                SnackbarType.PRIMARY ->
                    MySnackbar(
                        it,
                        containerColor = MaterialTheme.eColors.primary.light9,
                        contentColor = MaterialTheme.eColors.primary.base,
                        actionColor = MaterialTheme.eColors.primary.base,
                        actionContentColor = MaterialTheme.eColors.primary.light3,
                        dismissActionContentColor = MaterialTheme.eColors.primary.base,
                    )

                SnackbarType.INFO ->
                    MySnackbar(
                        it,
                        containerColor = MaterialTheme.eColors.info.light9,
                        contentColor = MaterialTheme.eColors.info.base,
                        actionColor = MaterialTheme.eColors.info.base,
                        actionContentColor = MaterialTheme.eColors.info.light3,
                        dismissActionContentColor = MaterialTheme.eColors.info.base,
                    )


                SnackbarType.SUCCESS ->
                    MySnackbar(
                        it,
                        containerColor = MaterialTheme.eColors.success.light9,
                        contentColor = MaterialTheme.eColors.success.base,
                        actionColor = MaterialTheme.eColors.success.base,
                        actionContentColor = MaterialTheme.eColors.success.light3,
                        dismissActionContentColor = MaterialTheme.eColors.success.base,
                    )


                SnackbarType.WARNING ->
                    MySnackbar(
                        it,
                        containerColor = MaterialTheme.eColors.warning.light9,
                        contentColor = MaterialTheme.eColors.warning.base,
                        actionColor = MaterialTheme.eColors.warning.base,
                        actionContentColor = MaterialTheme.eColors.warning.light3,
                        dismissActionContentColor = MaterialTheme.eColors.warning.base,
                    )


                SnackbarType.ERROR ->
                    MySnackbar(
                        it,
                        containerColor = MaterialTheme.eColors.danger.light9,
                        contentColor = MaterialTheme.eColors.danger.base,
                        actionColor = MaterialTheme.eColors.danger.base,
                        actionContentColor = MaterialTheme.eColors.danger.light3,
                        dismissActionContentColor = MaterialTheme.eColors.danger.base,
                    )
            }
        }
    }
}